# coding:utf-8
from marshmallow import fields

from app.op.models import UserInfo
from ..extensions import ma
from app.api.utils import EmptyStrValidator


class UserAvatarSchema(ma.Schema):
    img = fields.Str(required=True, error_messages={
        'required': 'img不合法'
    }, validate=EmptyStrValidator(error='img不合法'))

user_avatar_schema = UserAvatarSchema()


class UserEditSchema(ma.Schema):
    """
    created by chenhj at 2017/8/14

    """
    nickname = fields.Str()
    gender = fields.Int()
    addr = fields.Str()
    com = fields.Str()
    pos = fields.Str()
    area_id = fields.Integer()

user_edit_schema = UserEditSchema()


class UserInfoSchema(ma.Schema):
    """
    create by ld_wu 2017-11-21
    用户信息
    """
    id = fields.Integer()
    id_ = fields.Integer(attribute='id')
    nickname = fields.String()
    com = fields.String()
    avatar = fields.String(attribute='avatar_url')
    black = fields.Boolean()
    has_paid = fields.Boolean()
    active = fields.Integer()
    mobile = fields.String()
    type = fields.Integer()
    type_display = fields.String()
    level = fields.Integer(attribute='get_user_level')
    level_display = fields.String()
    status = fields.Integer()
    status_display = fields.String()
    has_set_business = fields.Boolean()
    half_price_paid = fields.Boolean()
    gender = fields.Integer()
    gender_display = fields.String()
    im_token = fields.String()
    addr = fields.String()
    pos = fields.String()
    prov = fields.String()
    phone_area_name = fields.Str(attribute='phone_area.name')
    credit = fields.Int(dump_only=True)
    info_bid_count = fields.Integer()
    city = fields.String(default='')
    level_3_notify = fields.Method('get_level_3_notify', dump_only=True)
    sms_alter_notify = fields.Method('get_sms_alter_notify', dump_only=True)
    enterprise_id = fields.Int(attribute='enterprise_info')
    pass_cnt = fields.Integer(attribute='pass_cnt')

    def get_level_3_notify(self, obj):
        """
        created by chenhj at 12/01/2018

        """
        return UserInfo.default_user_info(obj.id).level_3_notify

    def get_sms_alter_notify(self, obj):
        """
        add by zhouh 2018-07-04
        """
        return UserInfo.default_user_info(obj.id).sms_alter_notify


user_info_schema = UserInfoSchema()


class UserPackSchema(ma.Schema):
    """
    create by ld_wu 2017-11-21
    用户套餐
    """
    id = fields.Int()
    id_ = fields.Integer(attribute='id')
    uid = fields.Int()
    bid_point = fields.Int()
    call_point = fields.Int()
    accum_bid_point = fields.Int()
    accum_call_point = fields.Int()
    sms_point = fields.Int()  # 2018-04-11 弃用 by zhouhl
    accum_sms_point = fields.Int()
    # area_id = fields.List(fields.Int())
    area_id = fields.List(fields.Int, attribute='attention_area_ids')
    accum_dispatch_point = fields.Int()
    # 弃用end_date
    end_date = fields.Date()


user_pack_schema = UserPackSchema()


class UserAreaSchema(ma.Schema):
    """
    create by ld_wu 2017-11-21
    地域范围
    """

    area_id = fields.List(fields.Integer())
    area_type = fields.Boolean(default=False)

user_area_schema = UserAreaSchema()


class MobileSchema(ma.Schema):
    """
    create by ld_wu 2018-03-28
    手机号
    """

    mobile = fields.String()

mobile_schema = MobileSchema()


class UserCardSchema(ma.Schema):
    """
    create by ld_wu 2017-11-21
    用户信息
    """
    id = fields.Integer()
    id_ = fields.Integer(attribute='id')
    nickname = fields.String()
    com = fields.String()
    avatar = fields.String(attribute='avatar_url')
    mobile = fields.String()
    type = fields.Integer()
    type_display = fields.String()
    gender = fields.Integer()
    gender_display = fields.String()
    city = fields.String(default='')
    prov = fields.String(default='')
    pos = fields.String()
    is_friend = fields.Boolean(default=False)


user_card_schema = UserCardSchema(many=True)
